//
//  Hello_Box2dAppDelegate.m
//  Hello Box2d
//
//  Created by niko on 10-8-31.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import "Hello_Box2dAppDelegate.h"
#import "cocos2d.h"
#import "HelloWorldScene.h"
#import "CJSONSerializer.h"
#import "CJSONDeserializer.h"

@implementation Hello_Box2dAppDelegate

@synthesize window;

- (void) applicationDidFinishLaunching:(UIApplication*)application
{
	// CC_DIRECTOR_INIT()
	//
	// 1. Initializes an EAGLView with 0-bit depth format, and RGB565 render buffer
	// 2. EAGLView multiple touches: disabled
	// 3. creates a UIWindow, and assign it to the "window" var (it must already be declared)
	// 4. Parents EAGLView to the newly created window
	// 5. Creates Display Link Director
	// 5a. If it fails, it will use an NSTimer director
	// 6. It will try to run at 60 FPS
	// 7. Display FPS: NO
	// 8. Device orientation: Portrait
	// 9. Connects the director to the EAGLView
	//
	CC_DIRECTOR_INIT();
	
	// Obtain the shared director in order to...
	CCDirector *director = [CCDirector sharedDirector];
	
	// Sets landscape mode
	[director setDeviceOrientation:kCCDeviceOrientationLandscapeLeft];
	
	// Turn on display FPS
	[director setDisplayFPS:YES];
	
	// Turn on multiple touches
	EAGLView *view = [director openGLView];
	[view setMultipleTouchEnabled:YES];
	
	//-------
	//增加线性化对象
	/*CJSONSerializer *serializer = [[CJSONSerializer alloc] init];
	NSString* result = @"";
	NSNumber* n1 = [[[NSNumber alloc] initWithInt:900000] autorelease];
	
	result = [result stringByAppendingString:[[NSString alloc] initWithString:[serializer serializeObject:n1]]];
	NSNumber* b1 = [[[NSNumber alloc] initWithBool:false] autorelease];
	result = [result stringByAppendingString:[[NSString alloc] initWithString:[serializer serializeObject:b1]]];
	NSNumber* f1 = [[[NSNumber alloc] initWithDouble:0.0001] autorelease];
	result = [result stringByAppendingString:[[NSString alloc] initWithString:[serializer serializeObject:f1]]];
	
	//result = result + [[NSString alloc] initWithString:[serializer serializeObject:i1]];
	
	NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
	NSString *documentDir = [pathArray objectAtIndex:0];
	
	NSString *path = [documentDir stringByAppendingPathComponent:@"test.dat"];//@"/Users/niko/Desktop/test.dat";
	//NSString *path = @"/Users/niko/Desktop/test.dat";
	NSString *temp = @"Allen Dang";
	NSString *temp_1 = @"ZhaoGuoTaoSB";
	int i = 100;
	float f = 98.3333f;
	NSMutableData *writer = [[NSMutableData alloc] init];
	[writer appendData:[temp dataUsingEncoding:NSUTF8StringEncoding]];
	[writer appendBytes:&i length:sizeof(i)];
	[writer appendBytes:&f length:sizeof(f)];
	
	int nLength = [temp_1 length];
	[writer appendBytes:&nLength length:sizeof(nLength)];
	[writer appendData:[temp_1 dataUsingEncoding:NSUTF8StringEncoding]];
	
	nLength = [result length];
	[writer appendBytes:&nLength length:sizeof(nLength)];
	[writer appendData:[result dataUsingEncoding:NSUTF8StringEncoding]];
	
	[writer writeToFile:path atomically:YES];
	[writer release];
	
	
	int ii;
	float ff;
	int nLength2 = 0;
	int nNowPos = 0;
	NSString *ttemp;
	NSString *temp2;
	NSString* result1;
	NSData *reader = [NSData dataWithContentsOfFile:path];
	ttemp = [[NSString alloc] initWithData:[reader subdataWithRange:NSMakeRange(0, [temp length])]
								  encoding:NSUTF8StringEncoding];
	
	nNowPos += [temp length];
	[reader getBytes:&ii range:NSMakeRange(nNowPos, sizeof(ii))];
	nNowPos += sizeof(ii);
	[reader getBytes:&ff range:NSMakeRange(nNowPos, sizeof(ff))];
	nNowPos += sizeof(ff);
	[reader getBytes:&nLength2 range:NSMakeRange(nNowPos, sizeof(nLength2))];
	nNowPos += sizeof(nLength2);
	temp2 = [[NSString alloc] initWithData:[reader subdataWithRange:NSMakeRange(nNowPos, nLength2)]
								  encoding:NSUTF8StringEncoding];	
	nNowPos += nLength2;
	[reader getBytes:&nLength2 range:NSMakeRange(nNowPos, sizeof(nLength2))];
	
	nNowPos += sizeof(nLength2);
	result1 = [[NSString alloc] initWithData:[reader subdataWithRange:NSMakeRange(nNowPos, nLength2)]
								  encoding:NSUTF8StringEncoding];
	
	NSLog(@"string:%@  int:%i float:%f", ttemp, ii, ff);
	[temp release];
	[ttemp release];
	[path release];
	[temp2 release];
	[temp_1 release];
	[result release];
	[result1 release];
	[serializer release];*/
	
	
	
	// Default texture format for PNG/BMP/TIFF/JPEG/GIF images
	// It can be RGBA8888, RGBA4444, RGB5_A1, RGB565
	// You can change anytime.
	[CCTexture2D setDefaultAlphaPixelFormat:kTexture2DPixelFormat_RGBA8888];
		
	[[CCDirector sharedDirector] runWithScene: [HelloWorld scene]];
}


- (void)applicationWillResignActive:(UIApplication *)application {
	[[CCDirector sharedDirector] pause];
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
	[[CCDirector sharedDirector] resume];
}

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
	[[CCDirector sharedDirector] purgeCachedData];
}

-(void) applicationDidEnterBackground:(UIApplication*)application {
	[[CCDirector sharedDirector] stopAnimation];
}

-(void) applicationWillEnterForeground:(UIApplication*)application {
	[[CCDirector sharedDirector] startAnimation];
}

- (void)applicationWillTerminate:(UIApplication *)application {
	CC_DIRECTOR_END();
}

- (void)applicationSignificantTimeChange:(UIApplication *)application {
	[[CCDirector sharedDirector] setNextDeltaTimeZero:YES];
}

- (void)dealloc {
	[[CCDirector sharedDirector] release];
	[window release];
	[super dealloc];
}

@end

